﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FBProject.BusinessLogic.MSProjectProxies
{
    public class MSTasksProxy : IEnumerable<MSTaskProxy>
    {
        public class MSTasksProxyEnumerator : IEnumerator<MSTaskProxy>
        {
            private MSProjectProxy MSProjectProxy { get; set; }
            private IEnumerator MSTasksEnumerator { get; set; }

            public MSTasksProxyEnumerator(MSProjectProxy msProjectProxy, IEnumerator msTasksEnumerator)
            {
                _disposed = false;
                MSProjectProxy = msProjectProxy;
                MSTasksEnumerator = msTasksEnumerator;
            }

            public MSTaskProxy Current
            {
                get { return new MSTaskProxy(MSProjectProxy, (Microsoft.Office.Interop.MSProject.Task)MSTasksEnumerator.Current); }
            }

            object System.Collections.IEnumerator.Current
            {
                get { return Current; }
            }

            public bool MoveNext()
            {
                return MSTasksEnumerator.MoveNext();
            }

            public void Reset()
            {
                MSTasksEnumerator.Reset();
            }

            #region IDisposable Implementation

            private bool _disposed;

            public void Dispose()
            {
                Dispose(true);

                // Use SupressFinalize in case a subclass
                // of this type implements a finalizer.
                GC.SuppressFinalize(this);
            }

            protected virtual void Dispose(bool disposing)
            {
                // If you need thread safety, use a lock around these 
                // operations, as well as in your methods that use the resource.
                if (!_disposed)
                {
                    // Indicate that the instance has been disposed.
                    this.MSTasksEnumerator = null;
                    _disposed = true;
                }
            }

            #endregion
        }

        private MSProjectProxy MSProjectProxy { get; set; }
        private Microsoft.Office.Interop.MSProject.Tasks MSTasks { get; set; }

        public MSTasksProxy(MSProjectProxy msProjectProxy, Microsoft.Office.Interop.MSProject.Tasks msTasks)
        {
            MSProjectProxy = msProjectProxy;
            MSTasks = msTasks;
        }

        public IEnumerator<MSTaskProxy> GetEnumerator()
        {
            return new MSTasksProxyEnumerator(MSProjectProxy, MSTasks.GetEnumerator());
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public int Count
        {
            get { return MSTasks.Count; }
        }

        public MSTaskProxy this[int index]
        {
            get { return new MSTaskProxy(MSProjectProxy, MSTasks[index]); }
        }

        public MSTaskProxy Add(ExternalTask externalTask)
        {
            return new MSTaskProxy(MSProjectProxy, MSTasks.Add(externalTask.Name), externalTask);
        }
    }
}
